package org.steven.crm.core.service.dict;

import org.steven.crm.core.query.DictValueQueryDataList;
import org.steven.crm.core.service.BaseService;
import com.steven.xdb.dbmanager.DBManager;
import com.steven.xdb.pagination.query.AbstractQueryDataList;
import com.steven.xdb.session.DBSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wuzw
 * @Date 2021/5/24 21:30
 * @Version 1.0
 */
public class DictValueService extends BaseService {

    @Override
    protected String getTable() {
        return "dictValue";
    }

    @Override
    protected AbstractQueryDataList getQueryDataList() {
        return new DictValueQueryDataList();
    }

    @Override
    protected String getDataSourceName() {
        return null;
    }

    public Map<String, List<Map<String, String>>> readAllGroupByDictId() throws Exception {
        DBSession session = getSession();
        Map<String, String> k = new HashMap<>();
        k.put("deleted", "0");
        List<Map<String, String>> dictValues = DBManager.query(session, getTable(),
                "dictId, code, name", k, "order by sortIndex");

        Map<String, List<Map<String, String>>> dictValueMap = new HashMap<>();
        for (int i = 0; i < dictValues.size(); ++i) {
            Map<String, String> dictValue = dictValues.get(i);
            String dictId = dictValue.get("dictId");
            if (!dictValueMap.containsKey(dictId)) {
                dictValueMap.put(dictId, new ArrayList<>());
            }
            dictValueMap.get(dictId).add(dictValue);
        }

        return dictValueMap;
}

}
